package heima3_2.cn.osc.presenter;

import java.util.ArrayList;
import java.util.List;

import heima3_2.cn.osc.model.bean.GeneralNewsBean;
import heima3_2.cn.osc.ui.main_fragment.SyntheFragment;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/14 0014.
 */

public class SyntheHotspotPresenter extends BasePresenter<GeneralNewsBean> {
    private  SyntheFragment syntherMainFragment;

    int mFlag = 0;
    List<GeneralNewsBean.NewsBean> newslist = new ArrayList<>();
    public SyntheHotspotPresenter(SyntheFragment syntherMainFragment){
        this.syntherMainFragment=syntherMainFragment;
    }

    @Override
    protected void getFailureData(Throwable t) {
        t.printStackTrace();
    }

    @Override
    protected void getSuccessData(Response<GeneralNewsBean> response) {
        GeneralNewsBean body = response.body();
        if (mFlag == 0){
            newslist.removeAll(newslist);
            newslist = body.getNewslist();
            if (body.getNewslist().size() < 20) {
                syntherMainFragment.mLvSynthe.setHasMoreItems(false);
            }
        } else if (response.body().getNewslist().size() == 0){
            syntherMainFragment.mLvSynthe.setHasMoreItems(false);
        } else {
            newslist.addAll(body.getNewslist());
        }
        syntherMainFragment.mHotspotAdapter.setDatas(newslist);

        if (newslist.size() < mFlag * 20) { //如果加载到的数据不足页数乘以20，则说明已经没有数据
            syntherMainFragment.mLvSynthe.setHasMoreItems(false);
        }
    }

    @Override
    public void getData(int pager) {
        mFlag = pager;
        Call<GeneralNewsBean> info = mRetrofitInterface.getRecommendInfo(pager, 4, "week",20);
        info.enqueue(mCallback);
    }


}
